package com.dudu.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import org.apache.commons.lang.StringUtils;

public class DateUtils {

    public static String getTime(String mTime) {
	if (mTime == null || mTime.equals("")) {
	    return null;
	}

	try {
	    switch (isYeaterday(new java.sql.Date(Long.valueOf(mTime)))) {
	    case -1:// 今天返回时:分

		String currtData = new SimpleDateFormat("HH:mm").format(new java.sql.Date(Long.valueOf(mTime)));
		return currtData;

	    case 0:// 昨天 返回 昨天

		return "昨天";

	    case 1:// 至少是前天
		String qiantian = new SimpleDateFormat("MM-dd").format(new java.sql.Date(Long.valueOf(mTime)));
		return qiantian;

	    }
	} catch (ParseException e) {

	    e.printStackTrace();
	}

	return null;
    };

    /**
     * @author
     * @param oldTime
     *            较小的时间
     * @param newTime
     *            较大的时间 (如果为空 默认当前时间 ,表示和当前时间相比)
     * @return -1 ：同一天. 0：昨天 . 1 ：至少是前天.
     * @throws ParseException
     *             转换异常
     */
    private static int isYeaterday(Date oldTime) throws ParseException {
	// 将下面的 理解成 yyyy-MM-dd 00：00：00 更好理解点
	SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
	String todayStr = format.format(new Date());
	Date today = format.parse(todayStr);
	// 昨天 86400000=24*60*60*1000 一天
	if ((today.getTime() - oldTime.getTime()) > 0 && (today.getTime() - oldTime.getTime()) <= 86400000) {
	    return 0;
	} else if ((today.getTime() - oldTime.getTime()) <= 0) { // 至少是今天
	    return -1;
	} else { // 至少是前天
	    return 1;
	}

    }

    /**
     * @author 判断两个时间差是否大于零。
     * @param front
     *            前面的时间
     * 
     * @param behind
     *            后面的时间
     * 
     * @return true 是大于一分钟 false反之。
     * 
     */

    public static boolean IsShowTime(String front, String behind) {
	java.sql.Date mFrontDate = new java.sql.Date(Long.valueOf(front));
	java.sql.Date mBehindDate = new java.sql.Date(Long.valueOf(behind));

	// Long time = (mBehindDate.getTime() - mFrontDate.getTime());

	if ((mBehindDate.getTime() - mFrontDate.getTime()) > 0 && (mBehindDate.getTime() - mFrontDate.getTime()) >= 60 * 1000) {// 一分钟
	    return true;
	} else {
	    return false;
	}

    }


    /**
     * 获取当前时间
     * 
     */

    public static String getCurrentDate() {
	Date data = new Date(System.currentTimeMillis());

	return String.valueOf(data.getTime());

    }

    /**
     * 比较两个日期之间的大小
     * 
     * @param d1
     * @param d2
     * @return 前者大于后者返回true 反之false
     */
    public static boolean compareDate(Date d1, Date d2) {
	Calendar c1 = Calendar.getInstance();
	Calendar c2 = Calendar.getInstance();
	c1.setTime(d1);
	c2.setTime(d2);

	int result = c1.compareTo(c2);
	if (result >= 0)
	    return true;
	else
	    return false;
    }

    public static int getYear(String dateStr) {

	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	try {
	    Date date = sdf.parse(dateStr);
	    return date.getYear() + 1900;

	} catch (ParseException e) {
	    e.printStackTrace();
	}

	return -1;

    }

    public static String formatString(String dateStr) {

	String[] dates = dateStr.split("-");
	StringBuffer sb = new StringBuffer();
	sb.append(dates[0]);

	sb.append("-");
	if (dates[1].length() < 2) {
	    sb.append("0" + dates[1]);
	} else {
	    sb.append(dates[1]);
	}

	sb.append("-");
	if (dates[2].length() < 2) {
	    sb.append("0" + dates[2]);
	} else {
	    sb.append(dates[2]);
	}

	return sb.toString();

    }

    public static String formatYearMonth(String dateStr) {

	String[] dates = dateStr.split("-");
	StringBuffer sb = new StringBuffer();
	sb.append(dates[0]);

	sb.append("-");
	if (dates[1].length() < 2) {
	    sb.append("0" + dates[1]);
	} else {
	    sb.append(dates[1]);
	}

	return sb.toString();

    }

    public static int getMonth(String dateStr) {

	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	try {
	    Date date = sdf.parse(dateStr);
	    return date.getMonth() + 1;
	} catch (ParseException e) {
	    e.printStackTrace();
	}

	return -1;

    }

    /**
     * dateStr1
     * 
     * @param dateStr1
     *            开始日期
     * @param dateStr2
     *            结束日期
     * @return
     */
    public static List<String> getLstBetweenDates(String dateStr1, String dateStr2) {

	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

	List<String> list = new ArrayList<String>();

	try {
	    Date date1 = sdf.parse(dateStr1);
	    Date date2 = sdf.parse(dateStr2);

	    long time1 = date1.getTime();
	    long time2 = date2.getTime();

	    while (time1 <= time2) {

		list.add(sdf.format(time1));

		time1 += 24 * 60 * 60 * 1000;

	    }

	} catch (ParseException e) {
	    e.printStackTrace();
	}

	return list;

	/*
	 * 
	 * List<String> r = new ArrayList<String>(); String[] startStr =
	 * date1.split("-"); String[] endStr = date2.split("-"); int
	 * startYearInt = Integer.valueOf(startStr[0]); int startMonthInt =
	 * Integer.valueOf(startStr[1]); int startDayInt =
	 * Integer.valueOf(startStr[2]);
	 * 
	 * int endYearInt = Integer.valueOf(endStr[0]); int endMonthInt =
	 * Integer.valueOf(endStr[1]); int endDayInt =
	 * Integer.valueOf(endStr[2]);
	 * 
	 * 
	 * if(startYearInt==endYearInt){ if(startMonthInt==endMonthInt){
	 * if(startDayInt==endDayInt){ r.add(date1); }else{ for(int
	 * i=startDayInt;i<=endDayInt;i++){
	 * r.add(startYearInt+"-"+startMonthInt+"-"+i); } } }else{ for(int
	 * k=startMonthInt;k<=endMonthInt;k++){ if(k==startMonthInt){ int
	 * startCnts =
	 * getMonthDay(String.valueOf(startYearInt),String.valueOf(startMonthInt
	 * )); for(int
	 * j=startDayInt;j<=startCnts;j++)r.add(startYearInt+"-"+startMonthInt
	 * +"-"+j); }else if(k==endMonthInt){ for(int
	 * j=1;j<=endDayInt;j++)r.add(endYearInt+"-"+endMonthInt+"-"+j); }else{
	 * int otherCnts =
	 * getMonthDay(String.valueOf(startYearInt),String.valueOf(k)); for(int
	 * j=endDayInt;j<=otherCnts;j++)r.add(endYearInt+"-"+endMonthInt+"-"+j);
	 * } } } }else{ for(int k= startYearInt;k<=endYearInt;k++){
	 * if(k==startYearInt){ for(int i= startMonthInt;i<=12;i++){
	 * if(i==startMonthInt){ int otherCnts =
	 * getMonthDay(String.valueOf(startYearInt),String.valueOf(i)); for(int
	 * j=startDayInt;j<=otherCnts;j++)r.add(startYearInt+"-"+i+"-"+j);
	 * }else{ int otherCnts =
	 * getMonthDay(String.valueOf(startYearInt),String.valueOf(i)); for(int
	 * j=1;j<=otherCnts;j++)r.add(startYearInt+"-"+i+"-"+j); } } }else
	 * if(k==endYearInt){ for(int i= 1;i<=endMonthInt;i++){
	 * if(i==endMonthInt){ for(int
	 * j=1;j<=endDayInt;j++)r.add(endYearInt+"-"+i+"-"+j); }else{ int
	 * otherCnts =
	 * getMonthDay(String.valueOf(endYearInt),String.valueOf(i)); for(int
	 * j=1;j<=otherCnts;j++)r.add(endYearInt+"-"+i+"-"+j); } } }else{
	 * for(int i= 1;i<=12;i++){ int otherCnts =
	 * getMonthDay(String.valueOf(k),String.valueOf(i)); for(int
	 * j=1;j<=otherCnts;j++)r.add(k+"-"+i+"-"+j); } } } } return r;
	 */
    }

    // 获取某年某月的天数
    public static int getMonthDay(String yearStr, String monthStr) {
	int year = Integer.valueOf(yearStr);
	int month = Integer.valueOf(monthStr);
	int result = 0;
	switch (month) {
	case 1:
	case 3:
	case 5:
	case 7:
	case 8:
	case 10:
	case 12:
	    result = 31;
	    break;
	case 4:
	case 6:
	case 9:
	case 11:
	    result = 30;
	    break;
	case 2:
	    result = 28;
	    if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) {
		result = 29;
	    }
	    break;
	}
	return result;

    }

    public static long getCurrentDayTime() {

	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	String date1 = sdf.format(Calendar.getInstance().getTime());

	Date date2 = null;

	try {
	    date2 = sdf.parse(date1);
	} catch (ParseException e) {
	    e.printStackTrace();
	}

	return date2.getTime();

    }

    public static List<String> getYearLstBetweenDates(String date1, String date2) {

	List<String> result = new ArrayList<String>();
	String beginYear = date1.subSequence(0, 4).toString();
	String endYear = date2.subSequence(0, 4).toString();

	int ibeginYear = Integer.parseInt(beginYear);
	int iendYear = Integer.parseInt(endYear);

	for (int i = ibeginYear; i <= iendYear; i++) {

	    result.add(i + "");
	}

	return result;
    }

    public static List<String> getMonthLstBetweenDates(String date1, String date2) {

	String[] startStr = date1.split("-");
	String[] endStr = date2.split("-");
	int startYearInt = Integer.valueOf(startStr[0]);
	int startMonthInt = Integer.valueOf(startStr[1]);
	// int startDayInt = Integer.valueOf(startStr[2]);

	int endYearInt = Integer.valueOf(endStr[0]);
	int endMonthInt = Integer.valueOf(endStr[1]);
	// int endDayInt = Integer.valueOf(endStr[2]);

	List<String> r = new ArrayList<String>();
	if (startYearInt == endYearInt) {
	    if (startMonthInt == endMonthInt) {

		r.add(startYearInt + "-" + startMonthInt);

	    } else {

		for (int i = startMonthInt; i <= endMonthInt; i++) {
		    r.add(startYearInt + "-" + i);
		}
	    }
	} else {
	    for (int k = startYearInt; k <= endYearInt; k++) {

		if (k == startYearInt) {
		    for (int i = startMonthInt; i <= 12; i++) {
			r.add(k + "-" + i);
		    }
		} else if (k == endYearInt) {

		    for (int i = 1; i <= endMonthInt; i++) {

			r.add(k + "-" + i);
		    }
		} else {
		    for (int i = 1; i <= 12; i++) {
			r.add(k + "-" + i);
		    }
		}
	    }
	}

	return r;

    }

    public static void main(String[] args) {

	long crrent = getCurrentDayTime();

    }

    public static List<String> getDateBetweenTwo(String dateStr1, String dateStr2) {

	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

	List<String> list = new ArrayList<String>();

	try {
	    Date date1 = sdf.parse(dateStr1);
	    Date date2 = sdf.parse(dateStr2);

	    long time1 = date1.getTime();
	    long time2 = date2.getTime();

	    while (time1 <= time2) {

		list.add(sdf.format(time1));

		time1 += 24 * 60 * 60 * 1000;

	    }

	} catch (ParseException e) {
	    e.printStackTrace();
	}

	return list;

    }

    public static String getDateCurrentYear() {

	SimpleDateFormat sdf = new SimpleDateFormat("yyyy");

	Date now = new Date();

	String time = sdf.format(now);

	return time;

    }

    public static String getYestodayByFormat() {
	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	Calendar cal = Calendar.getInstance();
	cal.add(Calendar.DAY_OF_YEAR, -1);
	return sdf.format(cal.getTime());
    }

    /**
     * 得到本月的第一天
     * 
     * @return
     */
    public static String getMonthFirstDay() {

	DateFormat sf = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
	Calendar calendar = Calendar.getInstance();
	calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
	return sf.format(calendar.getTime());

    }

    /**
     * 得到本月的最后一天
     * 
     * @return
     */
    public static String getMonthLastDay() {

	DateFormat sf = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
	Calendar calendar = Calendar.getInstance();
	calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
	return sf.format(calendar.getTime());

    }

    public static String getMonth() {

	DateFormat sf = new SimpleDateFormat("MM", Locale.CHINA);
	Calendar calendar = Calendar.getInstance();
	calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
	return sf.format(calendar.getTime());

    }

    public static String getFormatTimeNow() {
	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
	return sdf.format(Calendar.getInstance().getTime());

    }

    public static String getTimeStringNow() {
	SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
	return sdf.format(Calendar.getInstance().getTime());

    }

    public static String getTimeAddBeginStr(String time) {
	return time + " 00:00:00";
    }

    public static String getTimeAddEndStr(String time) {
	return time + " 23:59:59";
    }

    public static String getTimeBeforeDays(int days) {
	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	Calendar cal = Calendar.getInstance();
	cal.add(Calendar.DAY_OF_MONTH, days);
	return sdf.format(cal.getTime());
    }

    /**
     * 某一时间加减一定的时间
     * 
     * @param date
     * @param field
     *            (Calendar.YEAR,MONTH,DATE,HOUR,MINUTE,SECOND)
     * @param amount
     * @return
     */
    public static Date afterDate(Date date, int field, int amount) {
	Calendar cal = Calendar.getInstance();
	cal.setTime(date);
	cal.add(field, amount);
	return cal.getTime();
    }

    public static String getFormatTime(Date Time, String pattern) {
	if (StringUtils.isBlank(pattern)) {
	    pattern = "yyyy-MM-dd HH:mm:ss";
	}
	SimpleDateFormat format = new SimpleDateFormat(pattern);
	String todayStr = format.format(Time);

	return todayStr;
    }

    /**
     * 2016-03-30 17:38:27.0
     * 
     * @return time:时间
     */
    public static Date getTimeByFormat(String time, String pattern) {
	if(StringUtils.isBlank(time)){
	    return null;
	}
	if (StringUtils.isBlank(pattern)) {
	    pattern = "yyyy-MM-dd HH:mm:ss";
	}
	SimpleDateFormat sdf = new SimpleDateFormat(pattern);
	try {
	    return sdf.parse(time);
	} catch (ParseException e) {
	    e.printStackTrace();
	}
	return null;
    }
    public static String getCurrentTime(){
	return getCurrentTime("");
    }
    public static String getCurrentTime(String pattern) {
	if (StringUtils.isBlank(pattern)) {
	    pattern = "yyyy-MM-dd HH:mm:ss";
	}
	SimpleDateFormat sdf = new SimpleDateFormat(pattern);
	return sdf.format(Calendar.getInstance().getTime());
    }

}
